package cn.com.yto56.coresystem.business.msg.soa.biz.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IGroupInfoBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IGroupInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IUserGroupInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.domain.GroupInfoBean;
import cn.com.yto56.coresystem.logic.msg.soa.domain.UserGroupInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by wangrui on 2017/2/16.
 */
@Service
public class GroupInfoBizImpl implements IGroupInfoBiz {
    @Autowired
    private IGroupInfoDao groupInfoDaoImpl;
    @Autowired
    private IUserGroupInfoDao userGroupInfoDaoImpl;

    @Override
    @Transactional
    public AppResult addUserGroup(String sysInfo_Id, String sys_userGroupId, String sys_userGroupName, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(sys_userGroupId)) {
            resultMessage = "参数[yto_msg_sys_userGroupId]不能为空";
        } else if (StringUtils.isBlank(sys_userGroupName)) {
            resultMessage = "参数[yto_msg_sys_userGroupName]不能为空";
        } else {
            GroupInfoBean groupInfoBean = new GroupInfoBean();
            groupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
            groupInfoBean.setYto_msg_sys_usergroupid(sys_userGroupId);
            groupInfoBean.setStatus("VALID");
            boolean checkUpdate = groupInfoDaoImpl.searchForUpdate(groupInfoBean);
            if (!checkUpdate) {
                resultMessage = "编号为[" + sys_userGroupId + "]的分组已经存在";
            } else {
                datas = new HashMap();
                String groupId = "MSG_GROUP_" + UUID.randomUUID().toString();
                groupInfoBean.setYto_msg_sys_usergroupname(sys_userGroupName);
                groupInfoBean.setYto_msg_sys_remark(sys_remark);
                groupInfoBean.setCreate_time(new Date());
                groupInfoBean.setYto_msg_usergroupid(groupId);
                int t = groupInfoDaoImpl.add(groupInfoBean);
                if (t <= 0) {
                    resultMessage = "[系统异常]sql执行失败";
                } else {
                    datas.put("yto_msg_userGroupId", groupId);
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    @Transactional
    public AppResult deleteUserGroup(String sysInfo_Id, String userGroupId, String status, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userGroupId)) {
            resultMessage = "参数[yto_msg_userGroupId]不能为空";
        } else {
            //安全删除
            if (StringUtils.isBlank(status)) {
                status = "0";
            }
            if (status.equals("0")) {
                UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
                userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userGroupInfoBean.setYto_msg_usergroupid(userGroupId);
                userGroupInfoBean.setStatus("VALID");
                boolean checkUpdate = userGroupInfoDaoImpl.searchForUpdate(userGroupInfoBean);
                if (!checkUpdate) {
                    resultMessage = "该组中包含用户，如果强制删除，请使用强制删除接口";
                } else {
                    GroupInfoBean groupInfoBean = new GroupInfoBean();
                    groupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                    groupInfoBean.setYto_msg_usergroupid(userGroupId);
                    groupInfoBean.setYto_msg_sys_remark(sys_remark);
                    int t = groupInfoDaoImpl.delete(groupInfoBean);
                    if (t <= 0) {
                        resultMessage = "删除失败或不存在[" + userGroupId + "]组";
                    } else {
                        resultFlag = true;
                        resultMessage = "执行成功";
                    }

                }
            } else {
                //强制删除
                UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
                userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userGroupInfoBean.setYto_msg_usergroupid(userGroupId);
                int t = userGroupInfoDaoImpl.delete(userGroupInfoBean);
                GroupInfoBean groupInfoBean = new GroupInfoBean();
                groupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                groupInfoBean.setYto_msg_usergroupid(userGroupId);
                groupInfoBean.setYto_msg_sys_remark(sys_remark);
                t = groupInfoDaoImpl.delete(groupInfoBean);
                if (t > 0) {
                    resultFlag = true;
                    resultMessage = "执行成功";
                } else {
                    resultMessage = "执行失败，或不存在该yto_msg_userGroupId";
                }

            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }
}
